package com.example.controller;


import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Ra;
import com.example.entity.PsLogs;
import com.example.entity.PsRole;
import com.example.mapper.PsRoleMapper;
import com.example.service.PsLogsService;
import com.github.pagehelper.PageHelper;
import com.example.service.impl.PsRoleServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static javafx.scene.input.KeyCode.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syj
 * @since 2024-04-22
 */
@CrossOrigin(origins="*")
@RestController
@RequestMapping("/ps-role")
public class PsRoleController {
    @Autowired
    private PsRoleServiceImpl psRoleService;
    @Autowired
    private PsRoleMapper psRoleMapper;
    @Autowired
    private PsLogsService psLogsService;
//    /**
//     * 查询全部
//     * @param psRole
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/all")
//    public Ra  all(PsRole psRole){
//        QueryWrapper<PsRole> psRoleQueryWrapper=new QueryWrapper<PsRole>();
//        System.out.println(psRole);
//
//        if(psRole.getRolename()!=null && psRole.getRolename()!="") {
//            psRoleQueryWrapper.like("roleName", psRole.getRolename());
//        }
//        if(psRole.getIsstart()!=null ) {
//            psRoleQueryWrapper.eq("isStart",psRole.getIsstart());
//        }
//        List<PsRole> list=psRoleService.list(psRoleQueryWrapper);
//        System.out.println(list);
//        long count = psRoleService.count();
//        return new Ra().AddDatamap("list",list).AddDatamap("count",count);
//    }
//
//
//    @GetMapping("/selectPage/{nowPage}")
//    public Ra select23(PsRole obj,@PathVariable("nowPage") Integer nowPage){
//        IPage<PsRole> page = new Page<>(nowPage,2);
//        QueryWrapper<PsRole> eQueryWrapper=new QueryWrapper<PsRole>();
//        eQueryWrapper.setEntity(obj);
//        page =this.psRoleService.page(page);
//        return new Ra().AddDatamap("fenye",page);
//    }

    @ResponseBody
    @RequestMapping("/selectPage")
    public Ra selectPage( Integer nowPage, PsRole psRole) {
        if(nowPage==null) {
            nowPage = 1;
        }
        IPage<PsRole> page = new Page<>(nowPage, 3);
        QueryWrapper<PsRole> psRoleQueryWrapper = new QueryWrapper<>();
        System.out.println(psRole.toString());
        if (psRole.getRolename() != null && !psRole.getRolename().equals("")) {
            psRoleQueryWrapper.like("roleName", psRole.getRolename());
        }
        if (psRole.getIsstart() != null) {
            psRoleQueryWrapper.eq("isStart", psRole.getIsstart());
        }
        IPage<PsRole> psRoleIPage = psRoleMapper.selectPage(page, psRoleQueryWrapper);
        List<PsRole> records = psRoleIPage.getRecords();
        long count = psRoleService.count();
        return new Ra().AddDatamap("fenye", records).AddDatamap("count",count);
    }

    /**
     * 添加角色
     * @param psRole
     * @return
     */
    @RequestMapping("/add")
    public  boolean AddPsRole(@RequestBody PsRole psRole,  PsLogs psLogs){
        System.out.println(psRole.getRolename());
        psRole.setCreationtime(LocalDateTime.now());
        psRole.setCreatedby("admin");
        boolean count=psRoleService.save(psRole);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("添加了角色任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return count;
    }

    /**
     * 修改角色
     * @param psRole
     * @return
     */
    @RequestMapping("/update")
    public  boolean UpdatePsRole(@RequestBody PsRole psRole,PsLogs psLogs){
        boolean count=psRoleService.updateById(psRole);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("添加了角色任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return   count;

    }

    @RequestMapping("/delete/{id}")
    public boolean deletePsRole( @PathVariable("id")int id,PsLogs psLogs){
        boolean count=psRoleService.removeById(id);
        if(count==true){
            System.out.println(psLogs.toString());
            psLogs.setUsercode("admin");
            psLogs.setUsername("管理员");
            psLogs.setOperateinfo("删除了角色任务");
            psLogs.setOperatedatetime(LocalDateTime.now());
            boolean count1=psLogsService.save(psLogs);
        }
        return   count;
    }


}
